<template>
    <div>
        <user_list @follow="follow" v-for="item, index in data.list" :item=item :index=index :key=index />
        <MyMessage :flag="alert_flag" :text="error_text" />
    </div>
</template>
<script setup>
import { defineAsyncComponent, ref, } from 'vue';
import { user_follow } from '../../api/api'
const user_list = defineAsyncComponent(() => import('../common/user_list.vue'))
const emit = defineEmits(['follow'])
const prop = defineProps(
    {
        data: {
            type: Object,
        }
    }
)
let alert_flag = ref(false)
let error_text = ref('')
function alertfn(text) {
    error_text.value = text
    alert_flag.value = true
    setTimeout(() => {
        alert_flag.value = false
    }, 2000)
}
function follow(obj) {
    user_follow(`api/user/${JSON.parse(localStorage.getItem('user')).userId}/triggerFollow/${obj.id}`).then(res => {
        if (res.data.code == 200) {
            if (res.data.data == '关注成功') {
                emit('follow', { text: 'follow', index: obj.index })
                alertfn('关注成功')
            } else {
                emit('follow', { text: 'none', index: obj.index })
                alertfn('取关成功')
            }
        }
    })
}
</script>
<style scoped lang="scss"></style>